/*
 * @lc app=leetcode.cn id=883 lang=typescript
 *
 * [883] 三维形体投影面积
 */

// @lc code=start
function projectionArea(grid: number[][]): number {
    const x_sum = grid.reduce((col, i) => col + Math.max(...i), 0);

    let longest = 0;
    const z_sum = grid.reduce((col, cur) => {
        longest = Math.max(longest, cur.length);
        // FIXME 俯视图高度为 1 时 不算
        return col + cur.filter((i) => i).length;
    }, 0);

    let y_sum = 0;
    for (let i = 0; i < longest; i++) {
        y_sum += Math.max(...grid.map((row) => row[i] || 0));
    }
    return x_sum + y_sum + z_sum;
}
// @lc code=end
